﻿using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using eAtlantida.Tools.Operations;

namespace eAtlantida.Tools
{
    public partial class Console
    {
        public static EnvDTE.DTE Dte;

        private static CommandLine commandLine;

        public Console()
        {
            InitializeComponent();
            commandLine = new CommandLine();
            commandLine.SendMessage += WriteMessage;

            WriteMessage("You must write connection string, for starting work. If you don't know how type 'help'.", MessageType.Request);
        }

        private void TextBoxKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                var text = txtConsole.Text;

                if (!ExecuteCommand(text))
                {
                    commandLine.ExecuteCommand(text);
                }
            }
        }

        private bool ExecuteCommand(string command)
        {
            switch (command)
            {
                case "clear":
                    txtConsole.Text = string.Empty;
                    tbDisplay.Document.Blocks.Clear();
                    return true;
                case "refresh":
                    return true;
                default:
                    return false;
            }
        }

        private void WriteMessage(string message, MessageType messageType = MessageType.Default)
        {
            txtConsole.Text = string.Empty;
            if (messageType == MessageType.Default)
            {
                tbDisplay.AppendText(message + "\n");
                txtConsole.Select(txtConsole.Text.Length, 0);
            }
            else
            {
                var color = Brushes.Black;
                switch (messageType)
                {
                    case MessageType.Warning:
                        color = Brushes.DarkOrange;
                        break;
                    case MessageType.Error:
                        color = Brushes.Red;
                        break;
                    case MessageType.Info:
                        color = Brushes.DarkGreen;
                        break;
                    case MessageType.Request:
                        color = Brushes.Blue;
                        break;
                }

                var tr = new System.Windows.Documents.TextRange(tbDisplay.Document.ContentEnd, tbDisplay.Document.ContentEnd);
                tr.Text = message + "\n";
                tr.ApplyPropertyValue(TextElement.ForegroundProperty, color);
            }
        }
    }
}